package com.biubiushop.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.alibaba.fastjson.JSONObject;
import com.biubiushop.entity.User;
import com.biubiushop.exception.ServiceException;
import com.biubiushop.repository.IUserRepository;
import com.biubiushop.service.IUserService;
import com.biubiushop.socket.nio.SocketChannelClient;
import com.biubiushop.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.Serializable;

/**
 * @author chenjia
 * @date 2019/6/24
 */
@RestController
@RequestMapping("/shareTissue")
@Api(tags = "用户登录注册验证模块")
public class UserController {

    private static final Log log = LogFactory.get();

    @Resource
    IUserService userService;

    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    IUserRepository userRepository;

    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Resource
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @ApiOperation(value = "小程序授权")
    @PostMapping("/user/v1/wxMiniAuthorization")
    @ApiImplicitParam(name = "code", value = "登录凭证(code)", required = true)
    public Serializable wxMiniAuthorization(String code) throws Exception {
        if (StrUtil.isEmpty(code)) {
            throw new ServiceException("code为空");
        }
        JSONObject json = userService.wxMiniAuthorization(code);
        return json;
    }

    @ApiOperation(value = "小程序登录")
    @PostMapping("/user/v1/wxMinilogin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "小程序用户openId", required = true),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = false),
            @ApiImplicitParam(name = "code", value = "验证码", required = false),
            @ApiImplicitParam(name = "nickName", value = "用户昵称", required = false),
            @ApiImplicitParam(name = "sex", value = "性别(1--男,2--女)", required = false),
    })
    public Serializable wxMinilogin(String openId, String phone, String code, String nickName, Integer sex) throws Exception {
        if (StrUtil.hasEmpty(openId)) {
            throw new ServiceException("参数为空");
        }
        UserLoginVO vo = userService.wxMiniLogin(openId, phone, code, nickName, sex);
        return vo;
    }

    @ApiOperation(value = "获取登陆注册验证码")
    @GetMapping("/user/v1/getRegisterSms")
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true)
    public Serializable getRegisterSms(String phone) throws Exception {
        if (StrUtil.hasEmpty(phone.trim())) {
            throw new ServiceException("参数为空");
        }
        String code = userService.getRegisterSms(phone);
        return code;
    }

    @ApiOperation(value = "app用户验证码登录")
    @PostMapping("/user/v1/appSmsLogin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", required = true)}
    )
    public Serializable appSmsLogin(String phone, String code) throws Exception {
        if (StrUtil.hasEmpty(phone, code)) {
            throw new ServiceException("参数为空");
        }
        User user = userService.appSmsLogin(phone, code);
        return user;
    }

    @PostMapping("/user/v1/applogin")
    public Serializable login(String phone, String password) throws Exception {
        if (StrUtil.hasEmpty(phone, password)) {
            throw new ServiceException("参数为空");
        }
        User user = userService.appLogin(phone, password);
        return user;
    }

    @ApiOperation(value = "根据信息生成二维码")
    @GetMapping("/admin/getSoleQrCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "info", value = "二维码中包含的信息", required = true),
            @ApiImplicitParam(name = "name", value = "二维码图片名称", required = true)}
    )
    public Serializable getQrCode(String info, String name) throws Exception {
        QrConfig config = new QrConfig();
        config.setBackColor(Color.white.getRGB());
        QrCodeUtil.generate(info, config, FileUtil.file("E:/" + name + ".jpg"));
        return true;
    }

    @ApiOperation(value = "测试get请求", notes = "这是一个测试get请求")
    @GetMapping("/test")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "length", value = "出纸长度"),
            @ApiImplicitParam(name = "id", value = "设备id")
    })
    public void getInfo(Integer length, String id) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("instructions", 1);
        jsonObject.put("length", length);
        jsonObject.put("id", id);
        jsonObject.put("orderId", "sasda14651");

        SocketChannelClient.start(jsonObject);
        return;
    }


    @ApiOperation(value = "测试get302请求", notes = "这是一个测试get302请求")
    @PostMapping("/test302")
    public void getInfo(HttpServletRequest request) throws Exception {
//        User user = new User();
//        user.setName("test09");
//        user.setSex(0);
//        user.setPhone("78945613269");
//        user.setCreateTime(new Date());
//        User o = userRepository.save(user);
//        User newUser = new User();
//        BeanUtil.copyProperties(user,newUser);
//        newUser.setName("ahjdlknalk");
//        userRepository.save(newUser);
    }

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("instructions", 1);
        jsonObject.put("length", 10);
        jsonObject.put("id", "89860441191840512520");
        jsonObject.put("orderId", "sasda14651");
        System.out.printf(jsonObject.toJSONString());
    }

}
